package com.lancoo.answer.view.result;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.bean.ExamViewBean;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.entity.AnswerSheetBean;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.ui.answerSheet.AnswerSheetAdapter;
import com.lancoo.answer.util.encodeUtils.TextUtils;
import com.lancoo.answer.util.paperUtils.PaperDataUtils;
import com.lancoo.answer.widget.CountNumberView;
import com.rxjava.rxlife.RxLife;

import java.util.List;
import java.util.Objects;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ExamResultFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ev_fragment_result_exam, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        TextView TvMyScoreRateTitle = view.findViewById(R.id.TvMyScoreRateTitle);// 总分
        CountNumberView TvMyScore = view.findViewById(R.id.TvMyScore);// 得分
        TextView TvFullScore = view.findViewById(R.id.TvFullScore);// 总分
        TextView TvUseTime = view.findViewById(R.id.TvUseTime);// 用时:--

        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if (taskControlBean == null) return;

        if (taskControlBean.getUserType() == 1) {
            TvMyScoreRateTitle.setText("学生得分");
        }

        ExamViewBean examViewBean = ConstantBean.Companion.getConstantExamBean();
        if (examViewBean == null) return;
        if(TextUtils.isEmpty(examViewBean.getScore())){
            examViewBean.setScore("0");
        }
        final float score = Float.parseFloat(examViewBean.getScore());
        TvMyScore.post(new Runnable() {
            @Override
            public void run() {
                String regex = score - (int) score == 0 ? CountNumberView.INTREGEX : CountNumberView.FLOATREGEX_1;
                TvMyScore.showNumberWithAnimation(score, regex);
            }
        });
        TvFullScore.setText(examViewBean.getHighestScore());
        TvUseTime.setText(examViewBean.getDuration());

        initRecyclerView(view);
    }


    private void initRecyclerView(View view) {
        Observable.create((ObservableOnSubscribe<List<AnswerSheetBean>>) emitter -> {
                    List<Type> typeList = ConstantBean.Companion.getConstantExamBean().getTypeList();
                    List<AnswerSheetBean> answerSheetBeanList = PaperDataUtils.loadAnswerSheetData(typeList, TaskState.LOOKING);
                    emitter.onNext(answerSheetBeanList);
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(RxLife.as(this))
                .subscribe(answerSheetBeanList -> {
                    RecyclerView recyclerView = view.findViewById(R.id.RvAnswerSheetParent);
                    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                    TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
                    int trainSence = taskControlBean.getTrainSence();
                    int paperType = taskControlBean.getPaperType();
                    if (paperType == 2) {
                        trainSence = 1;
                    }
                    AnswerSheetAdapter mAdapter = new AnswerSheetAdapter(answerSheetBeanList, trainSence);
                    mAdapter.setEnableShowAnswerScore(1);
                    recyclerView.setAdapter(mAdapter);
                }, throwable -> {});

    }
}
